//
//  CDepartmentMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/22.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CDepartmentMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CDepartmentModel.h"
@implementation CDepartmentMgr
//插入部门信息
-(BOOL)insertDepartment:(CDepartmentModel *)department
{
    if(department == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(department.DEPTID)) {
            return NO;
        }
    }
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select * from DEPT where  DEPTID = \'%@\'",department.DEPTID];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        if (result && [result next]) {
            ret = YES;
        }
        [result close];
        
        if (ret) {
            ret = [db executeUpdate:@"UPDATE DEPT SET UserNum = ?, Sequence = ?, DepartName = ?, DepartFatherID = ?, IsDel = ?, InfoState = ?,IsSelected = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where DEPTID = ?", @(department.UserNum), @(department.Sequence), M_STOREVALUE(department.DepartName), M_STOREVALUE(department.DeptFatherID), department.IsDel?@1:@0, M_STOREVALUE(department.InfoState),department.isSelected?@1:@0,M_STOREVALUE(department.yuLiuZiduan1),@(department.yuLiuZiduan2),M_STOREVALUE(department.yuLiuZiduan3),M_STOREVALUE(department.DEPTID)];

        } else {
            ret = [db executeUpdate:@"INSERT INTO DEPT(DEPTID, UserNum, Sequence, DepartName, DepartFatherID, IsDel, InfoState,IsSelected,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?,?,?,?,?)", M_STOREVALUE(department.DEPTID), @(department.UserNum), @(department.Sequence), M_STOREVALUE(department.DepartName), M_STOREVALUE(department.DeptFatherID), department.IsDel?@1:@0, M_STOREVALUE(department.InfoState),department.isSelected?@1:@0,M_STOREVALUE(department.yuLiuZiduan1),@(department.yuLiuZiduan2),M_STOREVALUE(department.yuLiuZiduan3)];
 
        }
    }];
    return ret;
}
- (BOOL)updateDepartment:(CDepartmentModel *)department
{
    if(department == nil)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE DEPT SET UserNum = ?, Sequence = ?, DepartName = ?, DepartFatherID = ?, IsDel = ?, InfoState = ?,IsSelected = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where DEPTID = ?", @(department.UserNum), @(department.Sequence), M_STOREVALUE(department.DepartName), M_STOREVALUE(department.DeptFatherID), department.IsDel?@1:@0, M_STOREVALUE(department.InfoState),department.isSelected?@1:@0,M_STOREVALUE(department.yuLiuZiduan1),@(department.yuLiuZiduan2),M_STOREVALUE(department.yuLiuZiduan3),M_STOREVALUE(department.DEPTID)];
    }];
    return ret;

}
//批量插入部门信息
-(BOOL)batchInsertDepartment:(NSArray *)departmentList
{
    if(departmentList == nil || [departmentList count] <= 0)
    {
        return NO;
    }
    
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CDepartmentModel *department in departmentList)
        {
            if (!M_ISSTRNULL(department.DEPTID)) {
                ret = [db executeUpdate:@"INSERT INTO DEPT(DEPTID, UserNum, Sequence, DepartName, DepartFatherID, IsDel, InfoState,IsSelected,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?,?,?,?,?)", M_STOREVALUE(department.DEPTID), @(department.UserNum), @(department.Sequence), M_STOREVALUE(department.DepartName), M_STOREVALUE(department.DeptFatherID), department.IsDel?@1:@0, M_STOREVALUE(department.InfoState),department.isSelected?@1:@0,M_STOREVALUE(department.yuLiuZiduan1),@(department.yuLiuZiduan2),M_STOREVALUE(department.yuLiuZiduan3)];                
                if(!ret)
                {
                    *rollback = YES;
                    break;
                }

            }
        }

    }];
   
    return ret;
}

//删除部门信息
-(BOOL)delDepartment:(CDepartmentModel *)department
{
    if(department == nil)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from DEPT where identifer = ?", @(department.DepartIndex)];
    }];
    return ret;
}


- (BOOL)delDepartmentWithDeptId:(NSString *)deptId
{
    if(deptId == nil)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from DEPT where DEPTID = ?", deptId];
    }];
    return ret;
}

- (BOOL)updateDepartmentIsdelWithDeptId:(NSString *)deptId IsDel:(BOOL)IsDel
{
    if(deptId == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE DEPT SET IsDel = ? where DEPTID = ?", IsDel?@1:@0,M_STOREVALUE(deptId)];
    }];
    return ret;
}
//删除所有部门信息
-(BOOL)delAllDepartment
{
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from DEPT"];
    }];
    return ret;
}

//获取部门信息
-(CDepartmentModel *)loadDepartment:(NSString *)departmentID
{
    
    if(M_ISSTRNULL(departmentID))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CDepartmentModel *department = nil;
    sql = [NSString stringWithFormat:@"select * from DEPT where  DEPTID = \'%@\'", departmentID ];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            department = [[CDepartmentModel alloc] init];
            department.DepartIndex = [result longLongIntForColumn:@"identifer"];
            department.DEPTID = [result stringForColumn:@"DEPTID"];
            department.UserNum = [result intForColumn:@"UserNum"];
            department.Sequence = [result intForColumn:@"Sequence"];
            department.DepartName = [result stringForColumn:@"DepartName"];
            department.DeptFatherID = [result stringForColumn:@"DepartFatherID"];
            department.IsDel = [result boolForColumn:@"IsDel"];
            department.InfoState = [result stringForColumn:@"InfoState"];
            department.isSelected = [result boolForColumn:@"isSelected"];
            department.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            department.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            department.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
        [result close];
    }];
    return department;
}

//根据父id获取部门信息
-(NSArray *)loadDepartments:(NSString *)fatherDepartmentID
{
    if(M_ISSTRNULL(fatherDepartmentID))
    {
        return nil;
    }
    NSString *sql = nil;
//     部门按降序排列 yh
    __block NSMutableArray *array = [NSMutableArray array];
    sql = [NSString stringWithFormat:@"select * from DEPT where  DepartFatherID = \'%@\' AND IsDel = 1 order by Sequence asc", fatherDepartmentID ];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CDepartmentModel * department = [[CDepartmentModel alloc] init];
            department.DepartIndex = [result longLongIntForColumn:@"identifer"];
            department.DEPTID = [result stringForColumn:@"DEPTID"];
            department.UserNum = [result intForColumn:@"UserNum"];
            department.Sequence = [result intForColumn:@"Sequence"];
            department.DepartName = [result stringForColumn:@"DepartName"];
            department.DeptFatherID = [result stringForColumn:@"DepartFatherID"];
            department.IsDel = [result boolForColumn:@"IsDel"];
            department.InfoState = [result stringForColumn:@"InfoState"];
            department.isSelected = [result boolForColumn:@"isSelected"];
            department.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            department.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            department.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            [array addObject:department];
            department = nil;
        }
        [result close];
    }];
    return array;
}

- (BOOL)updateAllDeptNotSelected:(NSArray *)deptList
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE DEPT SET IsSelected = 0"];
        
    }];
    return ret;
    
}
- (BOOL)updateSelectWithDeptList:(NSArray *)deptList isSelected:(BOOL)isSelected
{
    if(deptList == nil || deptList.count <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CDepartmentModel *dep in deptList)
        {
            ret = [db executeUpdate:@"UPDATE DEPT SET IsSelected = ? where DEPTID = ?", isSelected?@1:@0, M_STOREVALUE(dep.DEPTID)];
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    return ret;
    
}
- (BOOL)updateDeptSelectedWithDept:(CDepartmentModel *)dept seleceted:(BOOL)isSelected
{
    if(dept == nil)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE DEPT SET IsSelected = ? where DEPTID = ?", isSelected?@1:@0, M_STOREVALUE(dept.DEPTID)];
    }];
    return ret;
    
}

@end
